//
//  IncidentTopView.m
//  Runone
//
//  Created by runone02 on 16/8/26.
//  Copyright © 2016年 runone. All rights reserved.
//  事件管理主界面顶部滚动的view

#import "IncidentTopView.h"

@interface IncidentTopView ()
{
    NSInteger _lastTouchBtnTag;
}

@end

#define kScreenWidth   [UIScreen mainScreen].bounds.size.width
@implementation IncidentTopView

#pragma     ---------事件管理主界面


- (instancetype)initWithFrame:(CGRect)frame {
    if ([super initWithFrame:frame]) {
        self.backgroundColor = [UIColor cyanColor];
        [self configUI];
    }
    return self;
}
- (void)awakeFromNib {
    [super awakeFromNib];
    [self configUI];
}

- (void)configUI {
    NSArray *titleArray = @[@"当前事件",@"历史事件",@"收费事件",@"已报事件"];

    for (int i = 0; i < titleArray.count; i++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setTitle:titleArray[i] forState:UIControlStateNormal];
        btn.frame = CGRectMake(kScreenWidth / titleArray.count * i , 0, kScreenWidth / titleArray.count, 40);
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor purpleColor] forState:UIControlStateSelected];
        [btn addTarget:self action:@selector(moveUnderLine:) forControlEvents:UIControlEventTouchUpInside];
        btn.tag = 100 + i;
        btn.titleLabel.font = [UIFont systemFontOfSize:14];
        btn.titleLabel.textAlignment = NSTextAlignmentCenter;
        [self addSubview:btn];
    }
    UIButton *button = (UIButton *)[self viewWithTag:100];
    button.selected = YES;
    _lastTouchBtnTag = 100;
    
    self.underLine = [[UIView alloc] initWithFrame:CGRectMake(10,40, kScreenWidth / titleArray.count - 20 , 4)];
    self.underLine.backgroundColor = [UIColor purpleColor];
    [self addSubview:self.underLine];
}

- (void)moveUnderLine:(UIButton *)btn {
    [self changeBtnSelect:(btn.tag - 100)];
    if(self.btnClick) {
        self.btnClick(btn.tag);//deliver the of button's tag
    }
    [UIView animateWithDuration:0.25 animations:^{
        self.underLine.transform = CGAffineTransformMakeTranslation((btn.tag - 100) * kScreenWidth / 4, 0);
    }];
}
- (void)changeBtnSelect:(NSInteger)tag {
    UIButton *LastBtn = (UIButton *)[self viewWithTag:_lastTouchBtnTag];
    LastBtn.selected = NO;
    UIButton *currentBtn = (UIButton *)[self viewWithTag:tag + 100];
    currentBtn.selected = YES;
    _lastTouchBtnTag = tag + 100;
}

@end
